package xFGE.xDisplay
{
	import mx.core.UIComponent;
	import flash.display.DisplayObject;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	
	public class UIComponentAssistBase extends UIComponent
	{
		/**
		 * 热点，中心点，注册点 
		 */
		protected var _hotPoint:Point;
		
		/**
		 * 精灵的真正位置 
		 */
		protected var _spritePos:Point;
		
		protected var _allwayTopMost:DisplayObject;
		
		protected var _topMostSprite:DisplayObject;
		
		protected var _backMostSprite:DisplayObject;
		
		public function UIComponentAssistBase()
		{
			super();
			_hotPoint = new Point();
			_spritePos = new Point();
		}
		
		/**
		 * 设置叠加颜色 
		 * @param color
		 * 
		 */
		public function SetColor(color:int):void
		{
			var alpha:Number = Math.round((color >> 24 & 0xff)/255 * 100)/100;
			var red:Number = Math.round((color >> 16 & 0xff)/255 * 100)/100;
			var green:Number = Math.round((color >> 8 & 0xff)/255*100)/100;
			var blue:Number = Math.round((color&0xff)/255*100)/100;
			this.transform.colorTransform = new ColorTransform(red,green,blue,alpha);
		}
		
		/**
		 *设置旋转，涉及中心点(与bitmap属性中的rotation 不一样) 
		 * @param rotation 选择角度
		 * 
		 */
		public function SetRotation(rotation:Number):void
		{
			var oldPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			this.rotation = rotation;
			var newPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			x+=oldPos.x - newPos.x;
			y+=oldPos.y - newPos.y;
			
		}
		
		/**
		 *  设置放大缩小，涉及中心点(与bitmap属性中的scalex，scaley不一样)
		 * @param scaleX 横坐标放大量
		 * @param scaleY 纵坐标放大量
		 * 
		 */
		public function SetScale(scaleX:Number,scaleY:Number):void
		{
			var oldPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			this.scaleX = scaleX;
			this.scaleY = scaleY;
			var newPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			x+=oldPos.x - newPos.x;
			y+=oldPos.y - newPos.y;
		}
		
		/**
		 * 设置热点（中心点或注册点) 
		 * @param hotPoint
		 * 
		 */
		public function SetHotPoint(hotPoint:Point):void
		{
			var oldPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			_hotPoint = hotPoint;
			var newPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			x+= oldPos.x - newPos.x;
			y+= oldPos.y - newPos.y;
		}
		
		/**
		 * 翻转 
		 * @param x -1时为水平翻转
		 * @param y -1时为垂直翻转
		 * 
		 */
		public function Flip(x:int =1 ,y:int = 1):void
		{
			SetScale(scaleX*x,scaleY*y);
		}
		
		/**
		 * 置顶 
		 * 
		 */
		public function SetTopMost(obj:Object):void
		{
			var mc:DisplayObject = null; 
			if(obj is String ){
				if(obj == "blank")  return;
				mc = getChildByName((obj as String));
			}else if(obj is DisplayObject){
				mc = obj as DisplayObject;
			}
			if(mc){
				if(mc == _allwayTopMost) return;
				addChild(mc );
				_topMostSprite = mc;
			}
		}
		/**
		 * 设置永远置顶 
		 * @param name
		 * 
		 */
		public function SetAllwayTopMost(obj:Object):void
		{
			var mc:DisplayObject = null; 
			if(obj is String ){
				if(obj == "blank")  return;
				mc = getChildByName((obj as String));
			}else if(obj is DisplayObject){
				mc = obj as DisplayObject;
			}
			if(mc){
				if(_topMostSprite == mc) return;
				addChildAt(mc,numChildren-1);
				_topMostSprite = _allwayTopMost = mc;
				
			}
		}
		
		
		/**
		 * 取消永远置顶 
		 * 
		 */
		public function AllwayTopMostOff():void
		{
			_allwayTopMost = null;
		}
		/**
		 * 获取真正的sprite坐标 
		 * @return 
		 * 
		 */
		public function get spritePos():Point
		{
			return _spritePos;
		}
		
		/**
		 * 重写addChild 
		 * @param child
		 * @return 
		 * 
		 */
		override public function addChild(child:DisplayObject):DisplayObject
		{
			if(_allwayTopMost != null)
				return addChildAt(child,numChildren-2);
			return super.addChild(child);
		}

	}
}